/*
 *  ShapeWrapper.m
 *  pogostick
 *
 *  Created by daddy (chuck@kollar.com) on 9/11/09.
 *  Copyright 2009 Charles P. Kollar All rights reserved.
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#import "ShapeWrapper.h"

@implementation ShapeWrapper

@synthesize space;
@synthesize shape;
@synthesize contacts;
@synthesize data;
@synthesize value;
@synthesize isStaticShape;


-(id) initWithShape:(cpShape *)sh space:(cpSpace *)sp isStaticShape:(BOOL)iss
{
	return [self initWithShape:sh space:sp isStaticShape:iss contacts:nil data:nil value:0];
}
-(id) initWithShape:(cpShape *)sh space:(cpSpace *)sp isStaticShape:(BOOL)iss contacts:(cpContact *)c
{
	return [self initWithShape:sh space:sp isStaticShape:iss contacts:c data:nil value:0];
}
-(id) initWithShape:(cpShape *)sh space:(cpSpace *)sp isStaticShape:(BOOL)iss contacts:(cpContact *)c data:(void *)dat
{
	return [self initWithShape:sh space:sp isStaticShape:iss contacts:c data:dat value:0];
}
-(id) initWithShape:(cpShape *)sh space:(cpSpace *)sp isStaticShape:(BOOL)iss contacts:(cpContact *)c data:(void *)dat value:(int)val
{
	if (self = [super init]) {
		shape = sh;
        space = sp;
        isStaticShape = iss;
        
        contacts = c;
		data = dat;
		value = val;
	}
	return self;
}


#define DELETED_SHAPES_SIZE         80
cpShape *deletedShapes[DELETED_SHAPES_SIZE];
int deletedShapesI = 0, deletedPreviousMax = 0;

//
// This routine is usually called at the point of release.
// However, since the system doesn't always dealloc right away
// it is broken out so that the Chipmunk Physics stuff can be
// removed at the point of call...
-(void) cpFree {
    if (shape == nil) return;
    
    //NSLog(@"cpFree called shape: 0x%x body: 0x%x", shape, shape->body);
    
    for (int i=0; i<DELETED_SHAPES_SIZE; i++) {
        if (shape == deletedShapes[i]) {
            int j = (deletedShapesI < i) ? DELETED_SHAPES_SIZE - i + deletedShapesI : deletedShapesI - i;
            if (j > deletedPreviousMax) deletedPreviousMax = j;
            NSLog(@"BUG ALERT cpFree:: attempt to delete an already deleted shape: 0x%x previous=%d max=%d?!",
                  shape, j, deletedPreviousMax);
        }
    }
    
    if (isStaticShape == YES) {
        // shapes that are in static spaces don't have bodies, so don't try to free one...
        if (space != nil) {
            cpSpaceRemoveStaticShape(space, shape);
            cpSpaceRehashStatic(space);
        }
    } else {
        if (space != nil) {
            cpSpaceRemoveShape(space, shape);
            cpSpaceRemoveBody(space, shape->body);
        }
        // Delete the objects themselves...
        cpBodyFree(shape->body);
    }
    cpShapeFree(shape);
    
    deletedShapes[deletedShapesI] = shape;
    deletedShapesI = (deletedShapesI+1 >= DELETED_SHAPES_SIZE) ? 0 : deletedShapesI+1;
    
    space = nil; // so we don't try this again...
    shape = nil;
}

-(void) dealloc
{
    //[self cpFree];
    
	[super dealloc];
}

@end
